﻿/*
 *  This file is part of Worker Time.
 *
 *  Worker Time is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Worker Time is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Worker Time.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WorkerTime.Database;
using System.Data.SQLite;
using System.Globalization;
using System.Threading;
using WorkerTime.Utils;

namespace WorkerTime
{
    public partial class TracksManagerGUI : Form
    {
        private Manager database;
        private SQLiteConnection connection;
        private SQLiteCommand command;
        private SQLiteDataAdapter dataAdapter;
        private DataSet dataSet;
        private ComponentResourceManager localization;

        public TracksManagerGUI(Manager database)
        {
            localization = new System.ComponentModel.ComponentResourceManager(typeof(WorkerTimeStrings));
            this.database = database;
            InitializeComponent();
            this.Text = localization.GetString("String18") + " " + localization.GetString("String19");
        }

        private void ProjectManager_Load(object sender, EventArgs e)
        {
            ReadDatabase();
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns["ID"].Visible = false;
            dataGridView1.Columns["ProjectID"].Visible = false;
            dataGridView1.Columns["ActivityID"].Visible = false;
            dataGridView1.Columns["Project"].HeaderText = localization.GetString("String24");
            dataGridView1.Columns["Activity"].HeaderText = localization.GetString("String25");
            dataGridView1.Columns["Start"].DefaultCellStyle.Format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern; ;
            dataGridView1.Columns["Start"].HeaderText = localization.GetString("String26");
            dataGridView1.Columns["End"].DefaultCellStyle.Format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern; ;
            dataGridView1.Columns["End"].HeaderText = localization.GetString("String27");
            dataGridView1.Columns["Duration"].DefaultCellStyle.FormatProvider = new DurationFormatProvider();
            dataGridView1.Columns["Duration"].DefaultCellStyle.Format = "{0}";
            dataGridView1.Columns["Duration"].ReadOnly = true;
            dataGridView1.Columns["Duration"].HeaderText = localization.GetString("String28");
        }

        private void ReadDatabase()
        {
            connection = new SQLiteConnection("Data source=" + database.fileName);
            command = new SQLiteCommand(connection);
            command.CommandText = "SELECT Projects.Name As Project, Activity.Name As Activity, Tracks.ID As ID, Tracks.ProjectID As ProjectID, Tracks.ActivityID As ActivityID, Tracks.Start As Start, Tracks.End As End, Tracks.Duration As Duration FROM Tracks,Projects,Activity WHERE Tracks.ProjectID = Projects.ID AND Tracks.ActivityID = Activity.ID";
            dataAdapter = new SQLiteDataAdapter(command);
            dataSet = new DataSet("Tracks");
            dataAdapter.Fill(dataSet, "Tracks");
            dataGridView1.DataSource = dataSet.Tables["Tracks"];
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            Track track = new Track();
            TrackManagerEdit trackManagerEdit = new TrackManagerEdit(database, track);

            if (trackManagerEdit.ShowDialog() == DialogResult.OK)
            {
                track.Save(database);
                ReadDatabase();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditTrack();            
        }

        private void buttonApagar_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString()) > 0)
            {                
                Track track = TrackUtils.GetTrack(database, Int32.Parse(dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString()));
                track.Delete(database);
                int deletedRow = dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]);
                ReadDatabase();
                if (dataGridView1.Rows.Count >= 2 && (deletedRow - 1) >=1 )
                {
                    dataGridView1.Rows[deletedRow-1].Selected = true;
                }
                
            }
            
        }

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            EditTrack();
        }

        public void EditTrack()
        {
            Track track = TrackUtils.GetTrack(database, Int32.Parse(dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString()));
            TrackManagerEdit trackManagerEdit = new TrackManagerEdit(database, track);

            if (trackManagerEdit.ShowDialog() == DialogResult.OK)
            {
                track.Save(database);
                int editedRow = dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]);
                ReadDatabase();
                dataGridView1.Rows[editedRow].Selected = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewColumn col = dataGridView1.Columns[e.ColumnIndex];
            if (col.DefaultCellStyle.FormatProvider is DurationFormatProvider) 
            {
                Object originalData = e.Value;
                e.Value = String.Format(col.DefaultCellStyle.FormatProvider, col.DefaultCellStyle.Format, originalData);
            }

        }
    }
}	